﻿using System;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI;
using System.Xml;
using WeiBeeCommon.Core;

public partial class Crh723 : System.Web.UI.Page
{
    //string bigblacksilkurlstring = "http://t0.qlogo.cn/mbloghead/33c5f70c27b012e12550/0";
    // http://app.qlogo.cn/mbloghead/5a3ba83683c8cc1cb60a/0 
    // http://app.qlogo.cn/mbloghead/25628bb47663472e4af2/0 mediaroom

    protected void Page_Load(object sender, EventArgs e)
    {
        Session[Guid.NewGuid().ToString()] = 1;

        if (!string.IsNullOrEmpty(appkey) && !string.IsNullOrEmpty(appsecret))
        {
            OAuthQQ.SetConsumerKeyAndSecret(appkey, appsecret);
        }

        if (!Page.IsPostBack)
        {
            if (Request["oauth_verifier"] != null)
            {
                WeiBeeType t = WeiBeeType.QQ;
                if (Session["weibeetype"] != null)
                {
                    if (string.Compare(Session["weibeetype"].ToString(), t.ToString()) == 0)
                    {
                        AccessTokenGet(t);
                    }
                }
                Response.Redirect("crh723.aspx");
            }
            SwitchShow();
            //TestWaterMarkImage();
        }
    }

    private void SwitchShow()
    {
        if (Session[TokenSessionName] == null)
        {
            //lbLoginStatus.Text = "还没登录呢!";
            imageButtonLogin.Visible = true;
            //imageButtonSend.Visible = false;
        }
        else
        {
            imageButtonLogin.Visible = false;
            imgOringinal.Visible = true;
            imgResult.Visible = true;
            ShowLoginInfomation();
        }
    }

    protected void ShowLoginInfomation()
    {
        var weibee = WeiBeeFactory.CreateWeiBeeByType(WeiBeeType.QQ) as WeiBeeQQ;
        if (weibee != null)
        {
            weibee.SetOAuth(Session[TokenSessionName].ToString(),Session[TokenSecretSessionName].ToString());
            string result = weibee.GetWeiBeeUserInfo();
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(result);
            XmlNode headImageNode = xmlDoc.SelectSingleNode("/root/data/head");
            lbDebug.Text = headImageNode.InnerText;
            imgOringinal.ImageUrl = headImageNode.InnerText+"/0";
            string callargs = string.Format("WaterMakerHandler.ashx?url={0}&user={1}", imgOringinal.ImageUrl, Session[TokenSessionName]);
            imgResult.ImageUrl = callargs;//  "WaterMakerHandler.ashx?url=" + imgOringinal.ImageUrl;
        }
    }
    protected void TestWaterMarkImage()
    {
        imgResult.Visible = true;
        imgOringinal.Visible = true;
        imgOringinal.ImageUrl = "http://app.qlogo.cn/mbloghead/25628bb47663472e4af2/0";
        
        //imgResult.ImageUrl = callargs;//"WaterMakerHandler.ashx?url="+imgOringinal.ImageUrl;//"WaterMarkedImageUrl("http://app.qlogo.cn/mbloghead/25628bb47663472e4af2/0" + "/0");
    }

    protected void ImageButtonLoginClick(object sender, ImageClickEventArgs e)
    {
        OpenAuthPage();
    }

    private void AccessTokenGet(WeiBeeType wbType)
    {
        var weibee = WeiBeeFactory.CreateWeiBeeByType(wbType);
        weibee.SetOAuth(Session[TokenSessionName].ToString(), Session[TokenSecretSessionName].ToString());
        weibee.GetOAuth().Verifier = Request["oauth_verifier"];
        weibee.GetOAuth().AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
        Session[TokenSessionName] = weibee.GetOAuth().Token;
        Session[TokenSecretSessionName] = weibee.GetOAuth().TokenSecret;
    }

    void OpenAuthPage()
    {
        IWeiBee weibee = WeiBeeFactory.CreateWeiBeeByType(WeiBeeType.QQ);
        weibee.GetOAuth().SetCallbackUrl(callbackurl);
        string authenticationUrl = weibee.GetOAuth().AuthorizationLinkGet();
        Session[TokenSessionName] = weibee.GetOAuth().Token;
        Session[TokenSecretSessionName] = weibee.GetOAuth().TokenSecret;
        Session["weibeetype"] = weibee.UserType.ToString();
        Response.Redirect(authenticationUrl);
    }
    #region Configuration
    string TokenSessionName = "oauth_token";
    string TokenSecretSessionName = "oauth_token_secret";
    private string callbackurl = System.Configuration.ConfigurationManager.AppSettings["crh723"];
    private string appkey = System.Configuration.ConfigurationManager.AppSettings["appkey"];
    private string appsecret = System.Configuration.ConfigurationManager.AppSettings["appsecret"];
    #endregion
    protected void BtnChange_Click(object sender, EventArgs e)
    {
        var weibee = WeiBeeFactory.CreateWeiBeeByType(WeiBeeType.QQ) as WeiBeeQQ;
        if (weibee != null)
        {
            weibee.SetOAuth(Session[TokenSessionName].ToString(), Session[TokenSecretSessionName].ToString());
            var imgStream = new MemoryStream();
            var memoryStream = new StreamWriter(imgStream);
            var img = ImageCache.Images[Session[TokenSessionName].ToString()];
            string result = null;
            if (img != null)
            {
                img.Save(imgStream, img.RawFormat);
                imgStream.Position = 0;
                result = weibee.UpdateHead(imgStream);
                memoryStream.Close();
            }
            lbDebug.Text = result;
        }
    }
}